…t, id) for the batches list
The batches list page orders by createdAt DESC, id DESC filtered by
environment and a created-at window, but the only matching index was
(runtimeEnvironmentId, id), which cannot satisfy the createdAt ordering.
On environments with many batches the query fell back to a full table
scan and in-memory sort, which could run long enough to hit the
statement timeout.
Adds (runtimeEnvironmentId, createdAt DESC, id DESC) so the query reads
straight from the index in order. Created with CONCURRENTLY IF NOT
EXISTS, so it takes no table lock and is a no-op if already present.
Summary
The batches list page orders by
createdAt DESC, id DESCfiltered by environment and a created-at window, but the only supporting index onBatchTaskRunwas(runtimeEnvironmentId, id). That index can't satisfy thecreatedAtordering, so on environments with a large number of batches the query fell back to a full table scan and in-memory sort, which could run long enough to hit the statement timeout.Fix
Adds
(runtimeEnvironmentId, createdAt DESC, id DESC)onBatchTaskRun. The query now reads straight from the index in order with no sort step, returning a page with only a handful of heap fetches instead of scanning the whole environment slice.The migration uses
CREATE INDEX CONCURRENTLY IF NOT EXISTS, so it takes no table lock and is a no-op if the index already exists.